package org.jeecg.modules.storage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.storage.entity.BussProductStorage;
import org.jeecg.modules.storage.entity.BussProductStorageDetail;
import org.jeecg.modules.storage.mapper.BussProductStorageDetailMapper;
import org.jeecg.modules.storage.mapper.BussProductStorageMapper;
import org.jeecg.modules.storage.service.IBussProductStorageService;
import org.jeecg.modules.storage.vo.BussProductStoragePageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * @Description: 成品入库单
 * @Author: jeecg-boot
 * @Date:   2024-07-23
 * @Version: V1.0
 */
@Service
public class BussProductStorageServiceImpl extends ServiceImpl<BussProductStorageMapper, BussProductStorage> implements IBussProductStorageService {

	@Autowired
	private BussProductStorageMapper bussProductStorageMapper;
	@Autowired
	private BussProductStorageDetailMapper bussProductStorageDetailMapper;


	@Override
	public IPage<BussProductStoragePageVo> pageList(Page<BussProductStoragePageVo> page, QueryWrapper<BussProductStoragePageVo> queryWrapper) {
		return bussProductStorageMapper.pageList(page,queryWrapper);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(BussProductStorage bussProductStorage, List<BussProductStorageDetail> bussProductStorageDetailList) {
		bussProductStorageMapper.insert(bussProductStorage);
		if(bussProductStorageDetailList!=null && bussProductStorageDetailList.size()>0) {
			for(BussProductStorageDetail entity:bussProductStorageDetailList) {
				//外键设置
				entity.setProductStorageId(bussProductStorage.getId());
				bussProductStorageDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(BussProductStorage bussProductStorage,List<BussProductStorageDetail> bussProductStorageDetailList) {
		bussProductStorageMapper.updateById(bussProductStorage);
		
		//1.先删除子表数据
		bussProductStorageDetailMapper.deleteByMainId(bussProductStorage.getId());
		
		//2.子表数据重新插入
		if(bussProductStorageDetailList!=null && bussProductStorageDetailList.size()>0) {
			for(BussProductStorageDetail entity:bussProductStorageDetailList) {
				//外键设置
				entity.setProductStorageId(bussProductStorage.getId());
				bussProductStorageDetailMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		bussProductStorageDetailMapper.deleteByMainId(id);
		bussProductStorageMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			bussProductStorageDetailMapper.deleteByMainId(id.toString());
			bussProductStorageMapper.deleteById(id);
		}
	}
	
}
